/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package capanegocio;
import java.sql.*;
/**
 *
 * @author Coco
 */
public class chofer {
conectate con;
public chofer(){
    con=new conectate();
}
 public void newchofer(String rut,String nombre,String apellido,String sexo){
       try {            
           PreparedStatement pstm = con.getConnection().prepareStatement("insert into "+
                    "chofer(Rut_Chofer, Nombre, Apellido, Sexo_chofer) "+
                    "values(?,?,?,?)");
            pstm.setString(1, rut);
            pstm.setString(2, nombre);
            pstm.setString(3, apellido);
            pstm.setString(4, sexo);
            pstm.execute();
            pstm.close();
         }catch(SQLException e){
          System.out.println(e);
      }
   }
     public void updatechofer(String rut,String nombre,String apellido,String sexo){
       try {            
            PreparedStatement pstm = con.getConnection().prepareStatement("update chofer " +
            "set Nombre = ? ," +                    
            "Apellido = ? ,"  + 
            "Sexo_chofer = ? ,"  + 
            "where Rut_Chofer = ?");            
                
            pstm.setString(1, nombre);                        
            pstm.setString(2, apellido);
            pstm.setString(3, sexo);
            pstm.setString(4, rut);
            pstm.execute();
            pstm.close();            
         }catch(SQLException e){
         System.out.println(e);
      }
   }
      public void deletechofer(String rut){  
            try {                
                PreparedStatement pstm = con.getConnection().prepareStatement("delete from chofer where Rut_Chofer = ?");            
                pstm.setString(1, rut);                   
                pstm.execute();
                pstm.close();            
            }catch(SQLException e){
            System.out.println(e);
            }            
   }
      public Object [][] getDatos(){
      int registros = 0;
      //obtenemos la cantidad de registros existentes en la tabla
      try{         
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT count(1) as total FROM chofer");
         ResultSet res = pstm.executeQuery();
         res.next();
         registros = res.getInt("total");
         res.close();
      }catch(SQLException e){
         System.out.println(e);
      }
      
    Object[][] data = new String[registros][6];  
    //realizamos la consulta sql y llenamos los datos en "Object"
      try{    
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT " +
            " Rut_Chofer, Nombre, Apellido, Sexo_chofer " +
            " FROM chofer" +
            " ORDER BY Rut_Chofer ");
         ResultSet res = pstm.executeQuery();
         int i = 0;
         while(res.next()){
            String estCodigo = res.getString("Rut_Chofer");
            String estcod=res.getString("Nombre");
            String estNombre = res.getString("Apellido");
            String estpaterno = res.getString("Sexo_chofer");
            data[i][0] = estCodigo;            
            data[i][1] = estcod;            
            data[i][2] = estNombre;
            data[i][3] = estpaterno;
            i++;
         }
         res.close();
          }catch(SQLException e){
         System.out.println(e);
    }
    return data;
 }
}
